<?php
/**
 * Created by PhpStorm.
 * Author: kaodou
 * Date: 2017/10/17
 * Time: 15:07
 */
namespace App\Http\Admin\Program\Controllers;

use Framework\BaseClass\Http\Admin\Controller;
use App\Http\Admin\Website\Models\Menu;
use App\Http\Admin\Program\Models\Program;
use App\Eloquent\Ygt\Privilege;

class IndexController extends Controller
{
    public function setProgramJurisdiction()
    {
        if (request()->isMethod('post')) {
            $id = request('id');
            $role_jurisdictions = request('role_jurisdictions');
            $role_jurisdictionsStr = implode(',',$role_jurisdictions);
            $programModel = new Program();
            $updateData = ['role_jurisdiction'=>$role_jurisdictionsStr];
            $where = ['id'=>$id];
            $result = $programModel->updateOneData($where, $updateData);
            if($result){
                return $this->ajaxSuccess();
            }else{
                return $this->ajaxFail();
            }
        }


        $id = request('id');

        $adminUser          = request()->user('admin')->toArray();
        $company_id         = $adminUser['company_id'];

        $result         = collect(Privilege::getList( ['company_id'=>$company_id], 'id,title' )->toArray());

        $where = ['id'=>$id];
        $ProgramModel = new Program();
        $info = $ProgramModel->getOneData($where);

        $jurisdictions = explode(',',$info['role_jurisdiction']);

        $result->transform(function ($item, $key) use($jurisdictions) {
            $item['isCheck'] = in_array($item['id'],$jurisdictions) ? 1 : 0;
            return $item;
        });

        return $this->view('set-jurisdiction', compact('result', 'id'));

    }

}